package com.hymall.service.admin;

import com.hymall.bean.MarketUser;
import com.hymall.bean.MarketUserExample;
import com.hymall.bean.common.BasePageInfo;
import com.hymall.bean.common.CommonData;
import com.hymall.bean.common.User;
import com.hymall.mapper.MarketUserMapper;
import com.hymall.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hymall.service.admin.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author stone
 * @date 2022/06/25 11:11
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
//    UserMapper userMapper;

            MarketUserMapper marketUserMapper;


    /**
     * @return
     * @description 会员管理中通过模糊查询用户名，手机号
     */
    @Override
    public CommonData<MarketUser> query(String username, String mobile, BasePageInfo info) {
        PageHelper.startPage(info.getPage(), info.getLimit());
//        List<User> users = userMapper.select(username, mobile, info.getSort(), info.getOrder());
        MarketUserExample example = new MarketUserExample();
        MarketUserExample.Criteria criteria = example.createCriteria();

        if (username != null) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (mobile != null) {
            criteria.andMobileLike("%" + mobile + "%");
        }
        List<MarketUser> marketUsers = marketUserMapper.selectByExample(example);

        PageInfo<MarketUser> pageInfo = new PageInfo<>(marketUsers);
        return CommonData.data(pageInfo);

    }


    /**
     * @return list
     * @description 会员管理中用id查找
     */
    @Override
    public List<MarketUser> selectUserById(Integer id) {
        MarketUserExample example = new MarketUserExample();
        MarketUserExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);
        List<MarketUser> marketUsers = marketUserMapper.selectByExample(example);
        return marketUsers;
    }

    /**
     * @return a
     * @description 会员管理详情里面修改会员信息
     */
    @Override
    public void updateMessage(MarketUser marketUser) {

        MarketUserExample example = new MarketUserExample();

        MarketUserExample.Criteria criteria = example.createCriteria();

        criteria.andIdEqualTo(marketUser.getId());

        marketUserMapper.updateByExampleSelective(marketUser, example);

    }


    /*@Override
    public CommonData<User> query(String username, BasePageInfo info) {
        PageHelper.startPage(info.getPage(), info.getLimit());
        if (username != null) {
            username = "%" + username + "%";
        }
        List<User> users = userMapper.select(username, info.getSort(), info.getOrder());
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return CommonData.data(pageInfo);
    }*/
}
